#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QTimer>
#include <QMessageBox>
#include <QTranslator>

#include "qtcalc.h"
#include "varsfuncsdialog.h"
#include "updatechecker.h"
#include "dialogabout.h"
#include "dini/dini.h"
#include "calchistorydialog.h"

namespace Ui { class MainWindow; }

class MainWindow : public QMainWindow
{
    Q_OBJECT

    public:
        // Constructor & destructor
        MainWindow(QWidget* parent = 0);
        ~MainWindow();

    public slots:
        // Calculate the current expression
        void calculate();
        // Slot to be called when an expression is calculated, containing the result
        void calculated(const QString& msg, const bool& errorOccurred);
        // Displays an error message
        void displayErrorMessage(const QString& msg);

    signals:
        // Gives the command to calculate the given expression
        void calcString(const QString& expression);
        // Gives the command to save the settings of the calculator
        void saveCalculator();
        // Gives the command to load the settings of the calculator
        void loadCalculator();

        // Gives the command to recalculate the last expression
        void recalculate();

    protected:
        // The change event is reimplemented to handle the translate event
        void changeEvent(QEvent* e);

    private:
        Ui::MainWindow *ui;                                                     // The actual user interface, generated by Qt Creator using mainwindow.ui
        QTCalc calculator;                                                      // The calculator that receives all commands from this class

        QTimer* errorMessageTimer;                                              // Timer that's used to create a nice smooth animation for the error to fade away
        int errorMessageOpacity;                                                // Holds the current opacity of the animation

        varsFuncsDialog* myVarsFuncsDialog;                                     // A pointer to the dialog containing all variables and functions

        updates::updateChecker myUpdateChecker;                                 // Class to check for updates
        QMessageBox* updateWindow;                                              // Dialog to be displayed when a new update is available

        dialogAbout* aboutDialog;                                               // Dialog that displays info about Dalculator

        dini::iniFile settings;                                                 // Contains the settings of Dalculator, also used for loading and saving the settings

        QTranslator* englishTranslator;                                         // QTranslator used to translate the UI to English
        QTranslator* dutchTranslator;                                           // QTranslator used to translate the UI to Dutch

        calcHistoryDialog historyDialog;                                        // Dialog that holds the calculation history

        QString dataDir;                                                        // Directory containing the data, like the language files

        // Initialise all settings, and the calculator
        void initialise();

        // This event is used to make sure the settings are saved before closing the application
        void closeEvent(QCloseEvent* event);

        // Checks the right output type in the menu
        void checkOutputType();

    // The names of the private slots are self-explaining
    private slots:
        void on_actionShow_history_triggered();
        void on_actionScientific_triggered();
        void on_actionHexadecimal_triggered();
        void on_actionBinary_triggered();
        void on_actionOctal_triggered();
        void on_actionDecimal_triggered();
        void on_action_Radians_triggered();
        void on_action_Degrees_triggered();
        void on_action_Auto_detect_triggered();
        void on_actionTime_triggered();
        void on_actionHelp_triggered();
        void on_actionDutch_triggered();
        void on_actionEnglish_triggered();
        void on_actionAutocheck_for_updates_triggered(const bool& checked);
        void on_actionAbout_triggered();
        void on_actionCheck_for_updates_triggered();
        void on_showVarsFuncs_clicked();

        void errorMessageTimerEnded();
        void errorMessageCloseTimerEnded();
        void closeErrorMessage();

        void varsFuncsDialogClosed();

        void updatesChecked(const bool& newVersionAvailable);
        void updateWindowButtonClicked(QAbstractButton* button);
        void updateErrorOccurred(const bool& httpError);

        void settingLoadError(const QString& msg);

        void previousExpression();
        void nextExpression();
        void calcHistoryCleared();

        void setExpression(const QString& newExpr);
};

#endif // MAINWINDOW_H
